package com.book.service.impl;

import com.book.entity.BookInfo;
import com.book.mapper.BookInfoMapper;
import com.book.service.BookInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Service
public class BookInfoServiceImpl implements BookInfoService {
    @Resource
    private BookInfoMapper bookInfoMapper;
    //根据id删除书籍
    //逻辑删除,修改字段isdelete
    @Override
    public int changeBookInfoIsDeleted(BookInfo bookInfo) throws Exception {
        return bookInfoMapper.updateByPrimaryKeySelective(bookInfo);
    }
    //修改书籍信息
    //根据主键id修改信息
    @Override
    public int updateBookInfo(BookInfo bookInfo) throws Exception {
        return bookInfoMapper.updateByPrimaryKeySelective(bookInfo);
    }
    //查询书籍信息
    //根据书籍名模糊查询书籍信息
    @Override
    public List<BookInfo> selectBookByName(String bookName) throws Exception {
        Example example = new Example(BookInfo.class);
        example.createCriteria().andLike("bookName","%"+bookName+"%");
        return bookInfoMapper.selectByExample(example);
    }
    
    //分页操作
    @Override
    public PageInfo<BookInfo> selectBookInfoByPage(Integer pageNum, Integer pageSize) throws Exception {
        Page<BookInfo> page = PageHelper.startPage(pageNum, pageSize);
        bookInfoMapper.selectAll();
        return page.toPageInfo();
    }
    
    //根据发布时间和价格范围查询
    @Override
    public List<BookInfo> selectBookInfoConditions(BookInfo bookInfo, Date endPublisherDate, Double endPrice) throws Exception {
        Example example = new Example(BookInfo.class);
        Example.Criteria c = example.createCriteria();
        if (bookInfo.getBookName() != null && bookInfo.getBookName().length() != 0) {
            c.andLike("bookName", "%" + bookInfo.getBookName() + "%");
        }
        if (bookInfo.getPublisherDate() != null) {
            c.andBetween("publisherDate", bookInfo.getPublisherDate(), endPublisherDate);
        }
        if (bookInfo.getBookPrice() != null && bookInfo.getBookPrice().doubleValue() != 0) {
            c.andBetween("bookPrice", bookInfo.getBookPrice(), endPrice);
        }
        List<BookInfo> bookInfos = bookInfoMapper.selectByExample(example);
        return bookInfos;
    }
    
    //好书热卖模块，根据bookSaleCount倒序排序
    @Override
    public List<BookInfo> selectBookInfoByBookSaleCount() throws Exception {
        Example example = new Example(BookInfo.class);
        Example.Criteria c = example.createCriteria();
        example.setOrderByClause("book_sale_count DESC limit 8"); // 添加降序排序
        List<BookInfo> bookInfos = bookInfoMapper.selectByExample(example);
        return bookInfos;
    }
    
    //新书上架模块，根据出版时间倒序排序
    @Override
    public List<BookInfo> selectBookInfoByBookByPublisherDate() throws Exception {
        Example example = new Example(BookInfo.class);
        Example.Criteria c = example.createCriteria();
        example.setOrderByClause("publisher_date DESC limit 8"); // 添加降序排序
        List<BookInfo> bookInfos = bookInfoMapper.selectByExample(example);
        return bookInfos;
    }
    
    //根据收藏量排序
    @Override
    public List<BookInfo> selectBookInfoByBookCollectionCount() throws Exception {
        Example example = new Example(BookInfo.class);
        Example.Criteria c = example.createCriteria();
        example.setOrderByClause("book_collection_count DESC limit 8"); // 添加降序排序
        List<BookInfo> bookInfos = bookInfoMapper.selectByExample(example);
        return bookInfos;
    }
    
    //根据id查询书籍信息
    @Override
    public BookInfo selectBookInfoById(Integer bookId) throws Exception {
        return bookInfoMapper.selectByPrimaryKey(bookId);
    }
    
    
}
